package com.atguigu03.buffered.exer;

import org.junit.Test;

import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImageFilter;
import java.io.*;

/**
 * ClassName: BufferedExerTest
 * Package: com.atguigu03.buffered.exer
 * Description:
 *
 * @Author BuTang
 * @Create 2023/6/14 19:08
 * @Version 1.0
 */
public class BufferedExerTest {

@Test
public void test1(){
    BufferedExerTest b = new BufferedExerTest();
//    File f1 = new File("C:\\Users\\11921\\Desktop\\学习\\视频47-流程控制-for循环结构的基本使用.mp4");
//    File f2 = new File("C:\\Users\\11921\\Desktop\\视频47-流程控制-for循环结构的基本使用.mp4");
    long start = System.currentTimeMillis();
    b.fileInputMethod("D:\\WorkSpace\\ceshi.mp4","C:\\Users\\11921\\Desktop\\视频47-流程控制-for循环结构的基本使用.mp4");
    long end = System.currentTimeMillis();
    System.out.println(end-start);
}


    //提供一个使用FileInputStream和FileOutputStream实现非文本文件复制的方法

    public void fileInputMethod(String s1,String s2){
        File file1 = new File(s1);
        File file2 = new File(s2);

        //创建两个字符流
        FileInputStream f1 = null;
        FileOutputStream f2 = null;
        try {
            f1 = new FileInputStream(file1);
            f2 = new FileOutputStream(file2);

            //创建并且设置缓冲区
            byte[] buffer = new byte[50];
            int len;
            while((len = f1.read(buffer)) != -1){
                f2.write(buffer,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }   finally {

            try {
                if ( f2 != null)
                f2.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if ( f1 != null)
                f1.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    @Test
    public void test2(){

    BufferedExerTest b = new BufferedExerTest();
        long start = System.currentTimeMillis();
        b.BufferIntputAndOutput("D:\\WorkSpace\\ceshi.mp4","C:\\Users\\11921\\Desktop\\视频47-流程控制-for循环结构的基本使用.mp4");
        long end = System.currentTimeMillis();
        System.out.println(end-start);

    }

    //提供一个使用BufferedInputStream和BufferedOutputStream实现非文本文件复制的方法
    public void BufferIntputAndOutput(String s1,String s2){
        File file1 =  new File(s1);
        File file2 = new File(s2);

        BufferedInputStream f1 = null;
        BufferedOutputStream f2 = null;
        try {
            FileInputStream fi1= new FileInputStream(file1);
            FileOutputStream fil2 = new FileOutputStream(file2);

            f1 = new BufferedInputStream(fi1);
            f2 = new BufferedOutputStream(fil2);

            byte[] buffer = new byte[50];
            int len = 0;
            while ((len = f1.read(buffer)) != -1){
                f2.write(buffer,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {

            try {
                if ( f1 != null)
                f1.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if ( f2 != null)
                f2.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
     }
}
